package com.whsr.readservice.dao;

import com.whsr.readservice.vo.PageConfig;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface PageConfigMapper {

    @Delete("DELETE FROM page_config WHERE group_id in ( SELECT id FROM page_config_group WHERE parent_id =#{id})")
    int removeConfigByParentId(int id);


    @Select({"<script>",
        "select CONCAT('c', c.id) as id, a.book_name as name, CONCAT('p', c.group_id) as parentId, a.book_cover as bookImg from page_config c, article a where a.id = c.art_id ",
        "<when test='name != null'>",
        " and a.book_name = #{name} ",
        "</when>",
        " union ",
        " select CONCAT('p', g.id) as id, g.name as name, CONCAT('p', g.parent_id) as parentId, '' as bookImg from page_config_group g where g.data_flag = 1 ",
        "<when test='name != null'>",
        " and a.name = #{name} ",
        "</when>",
        "</script>"})
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "parentId", column = "parentId"),
            @Result(property = "bookImg", column = "bookImg")
    })
    List<Map<String, Object>> queryConfigList(Object name);

    @Select("select * from page_config where id=#{id}")
    @Results({
            @Result(property = "groupId", column = "group_id"),
            @Result(property = "artId", column = "art_id"),
            @Result(property = "toUrl", column = "to_url"),
            @Result(property = "dataFlag", column = "data_flag"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    PageConfig getConfigById(int id);

    @Insert("insert into page_config (id, group_id, art_id, to_url, data_flag, create_time, update_time) values (#{id}, #{groupId}, #{artId}, #{toUrl}, #{dataFlag}, #{createTime}, #{updateTime})")
    @SelectKey(statement="SELECT LAST_INSERT_ID()",keyProperty="id",before=false,resultType=Integer.class)
    int update(PageConfig pageConfig);

    @Update("update page_config set group_id=#{groupId}, art_id=#{artId}, to_url=#{toUrl}, data_flag=#{dataFlag}, create_time=#{createTime}, update_time=#{updateTime} where id=#{id}")
    int add(PageConfig pageConfig);

    @Delete("DELETE FROM page_config WHERE group_id in ( SELECT id FROM page_config_group WHERE id =#{id})")
    int removeConfig(int id);

    @Select("select id, name from page_config_group where position=#{type} and parent_id=0 and data_flag=1")
    List<Map<String,Object>> getIndexTab(String type);

    @Select("select  id as id, name as name, parent_id as parentId, group_icon as iconUrl, templet as templet, to_url as toUrl from page_config_group where parent_id =#{parent_id}")
    List<Map<String,Object>> getMobileIndex(int parent_id);

//    @Select("select  id as id, name as name, parent_id as parentId, group_icon as iconUrl, templet as templet from page_config_group where parent_id =#{parentId} "
//            + " union " +
//            " select a.id as id, b.book_name as name,  a.group_id as parentId, b.book_cover as iconUrl, '' as templet from page_config a ,article b where a.group_id=#{parentId} and a.art_id = b.id and a.source=1 "
//            + " union " +
//            " select a.id as id, b.name as name,  a.group_id as parentId, '' as iconUrl, '' as templet from page_config a ,category b where a.group_id=#{parentId} and a.art_id = b.id and a.source=2 ")
//    @Select("select  a.id as id, b.book_name as name, a.parent_id as parentId, case a.templet when 'carousel' then CONCAT('/api/server/file/imgView/', a.group_icon) else b.book_cover end as iconUrl, '' as templet, a.to_url as toUrl from page_config_group a, article b where a.to_url = b.book_id and parent_id = #{parentId} limit 0, 3")
    @Select("select  a.id as id, b.book_name as name, a.parent_id as parentId, case when a.templet = 'carousel' and a.group_icon like 'http%' then a.group_icon when  a.templet = 'carousel' and a.group_icon not like 'http%' then CONCAT('/api/server/file/imgView/', a.group_icon) else b.book_cover end as iconUrl, '' as templet, a.to_url as toUrl from page_config_group a, article b where a.to_url = b.book_id and a.data_flag = 1 and parent_id = #{parentId} limit 0, 3")
    List<Map<String,Object>> getArtChildren(int parentId);

    @Select("select  a.id as id, a.name as name, a.parent_id as parentId, '' as iconUrl, '' as templet, a.to_url as toUrl from page_config_group a where a.data_flag = 1 and parent_id = #{parentId}")
    List<Map<String,Object>> getTypeChildren(int parentId);

    @Select("select  a.id as id, a.name as name, a.parent_id as parentId, CONCAT('/api/server/file/imgView/', a.group_icon) as iconUrl, '' as templet, a.to_url as toUrl from page_config_group a where a.data_flag = 1 and parent_id = #{parentId}")
    List<Map<String,Object>> getButtonChildren(int parentId);
}
